﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace peonwar
{
    public class AnimatedEffect : AnimatedSprite
    {
        public AnimatedEffect(float x, float y, bool TeamBlue, Texture2D texture, int imgwidth, int imgheight, int indexmax) : base()
        {
            Position = new Vector2(x, y);
            this.TeamBlue = TeamBlue;
            this.Imgwidth = imgwidth;
            this.Imgheight = imgheight;
            this.Indexmax = indexmax;
            this.Texture = texture;
            this.Animationspeed = 0.17;
            // Miroir des sprites en fonction de l'equipe
            if (TeamBlue)
            {
                this.Effect = SpriteEffects.FlipHorizontally;
            }
            else
            {
                this.Effect = SpriteEffects.None;
            }
        }

        private bool TeamBlue;

        public override void UpDate(GameTime gameTime)
        {
            // Si l'animation joue alors :
            if (Play)
            {
                // Tant que l'index n'a pas depasse l'index max on l'incremente l'index de l'animationspeed
                Index += Animationspeed;
                if (Index >= Indexmax + 1)
                    Play = false;
            }
        }

        public override void Draw(SpriteBatch spriteBatch, Camera camera)
        {
            int c = 0;
            if (camera != null)
                c = camera.Position;

            // La "Position" est au centre du sprite
            int newindex = (int)Math.Floor(Index);
            Rectangle Desti = new Rectangle((int)Position.X - (Imgwidth / 2) - c, (int)Position.Y - Imgheight / 2, Imgwidth, Imgheight);
            Rectangle Source = new Rectangle((newindex * Imgwidth), 0, Imgwidth, Imgheight);
            spriteBatch.Draw(Texture, Desti, Source, Color.White, 0f, Vector2.Zero, Effect, 0f);
        }
    }
}
